//
//  FirstDetailsViewController.m
//  Fragrance
//
//  Created by yinzhongyuan on 15/5/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FirstDetailsViewController.h"

@interface FirstDetailsViewController ()

@end

@implementation FirstDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = self.name;
    
//    self.hidesBottomBarWhenPushed = YES;
//    self.navigationController.hidesBarsOnTap = YES;
    
    [TAOverlay showOverlayWithLabel:nil Options:TAOverlayOptionOverlaySizeRoundedRect];
    
    
//提示的同时加载数据
    [self downLoadThisPage];

   
}


- (void)shangla
{
    if (i == 0) {
        i = i + 2;
         [self downLoadThisPage];
        [firstDetailsTableView footerEndRefreshing];
    }else{
    
    i++;
    [self downLoadThisPage];
    }
    [firstDetailsTableView footerEndRefreshing];
    
}

- (void)xiala
{
    if (i > 1) {
        i--;
        [self downLoadThisPage];
        [firstDetailsTableView headerEndRefreshing];
    }
    [firstDetailsTableView headerEndRefreshing];
}


- (void)initMyTableView
{
    firstDetailsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    firstDetailsTableView.delegate = self;
    firstDetailsTableView.dataSource = self;
    [firstDetailsTableView addFooterWithTarget:self action:@selector(shangla)];
    [firstDetailsTableView addHeaderWithTarget:self action:@selector(xiala)];
    [self.view addSubview:firstDetailsTableView];
    [firstDetailsTableView release];
}


-(void)downLoadThisPage
{
    
        NSString *str = [NSString stringWithFormat:@"http://api.2meiwei.com/v1/shicai/ingredient/?id=%@&idx=%ld&size=15&appname=mw_android&appver=1.0.1&osver=4.4.2&devicename=H60-L01&openudid=866568021277461", self.ID, i];
    
    
    NSLog(@"%ld", i);
    
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        [TAOverlay hideOverlay];
        //数据请求完成，开始解析
        self.arraydata = [NSMutableArray array];
        self.arraydata = [FDJieXi getDetailsModel:data];
        
       
        //数据源准备完成，可以添加tableview
        [self initMyTableView];
       
        
    }];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"cell";
    FirstDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[FirstDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    model = [self.arraydata objectAtIndex:indexPath.row];
    
    UIImage *image = [UIImage imageNamed:@"beijing3"];
    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
    cell.backgroundView = imageview;
    
    
    [cell.piture setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"jiazaitu"]];
    
    cell.nameLabel.text = model.name;
    cell.yongliao.text = model.need;
    
   
    NSString *str = [NSString stringWithFormat:@"收藏:%ld", (long)model.saveNumber];
    cell.savenum.text = str;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arraydata.count;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 110;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    model = [self.arraydata objectAtIndex:indexPath.row];
    
    FDNextViewController *FDnext = [[FDNextViewController alloc] init];
    FDnext.ID = model.Id;
    FDnext.picIcon = model.pic;
    

    [self.navigationController pushViewController:FDnext animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
